package the.broccoli.setting.service.impl;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.wisteria.framework.core.exception.BusinessException;
import the.broccoli.setting.entity.Params;
import the.broccoli.setting.mapper.ParamsMapper;
import the.broccoli.setting.service.IParamsService;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 参数 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2019-11-27
 */
@Service
public class ParamsServiceImpl extends AbstractSuperService<ParamsMapper, Params> implements IParamsService {

    /**
     * 获取参数值
     *
     * @param paramsCode 参数编码
     * @return 参数值
     */
    @Override
    public String getValue(String paramsCode) {
        if (StringUtils.hasText(paramsCode)) {
            Params entity = this.baseMapper.selectByCode(paramsCode);
            if (entity != null) {
                return entity.getParamsValue();
            }
        }
        return null;
    }

    @Transactional
    @Override
    public boolean save(Params entity) {
        checkParams(entity);
        return super.save(entity);
    }

    @Transactional
    @Override
    public boolean updateById(Params entity) {
        checkParams(entity);
        return super.updateById(entity);
    }

    private void checkParams(Params entity) {
        if (!StringUtils.hasText(entity.getParamsCode())) {
            throw new BusinessException("参数编码不能空");
        }

        if (this.baseMapper.selectByCode(entity.getParamsCode()) != null) {
            throw new BusinessException("参数编码已经存在相同的");
        }
    }
}
